package com.huang.consumer_mall.web;

import com.github.pagehelper.PageInfo;
import com.huang.consumer_mall.model.Customer;
import com.huang.consumer_mall.model.PageCondition;
import com.huang.consumer_mall.service.CustomerService;
import com.huang.consumer_mall.util.EmailTest;
import com.huang.consumer_mall.util.EmailUtils;
import com.huang.consumer_mall.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @PostMapping("/api/login")
    public Map<String,Object> login(@RequestBody Customer customer, HttpServletResponse response){

        Map<String,Object> map =new HashMap<>();
        Customer customer1=customerService.find(customer);
        if(customer1!= null && customer1.getCusStatus().equals("2")){
            map.put("state",false);
            map.put("msg","账号已被冻结");
            return map;
        }
        if (customer1!=null && passwordEncoder.matches(customer.getCusPwd(),customer1.getCusPwd())){

                map.put("cusId",customer1.getCusId());
                map.put("cusName",customer1.getCusName());
                map.put("cusAccount",customer1.getCusAccount());
                String token = JwtUtils.getToken(customer1);
                map.put("state",true);
                map.put("token",token);
                return map;
        }
        map.put("state",false);
        map.put("msg","账户密码错误");
        return map;
    }
    @PostMapping("/api/getCode")
    public String  getCode(@RequestBody Customer customer) throws MessagingException {
        String code = EmailTest.getCode();
        EmailUtils.send_mail(customer.getCusEmail(),code);
        return code;
    }
    @PostMapping("/api/regist")
    public String regist(@RequestBody Customer customer){
        String str =passwordEncoder.encode(customer.getCusPwd());
        customer.setCusPwd(str);
        customerService.insert(customer);
        return "success";
    }

    @PostMapping("/api/user/customerAdd")
    public String regist1(@RequestBody Customer customer) {

        if(customer.getCusId()!=null){
            Customer customer1 = customerService.find(customer);
            if(customer.getCusPwd().equals(customer1.getCusPwd())){
                customerService.update(customer);
                return "success2";
            }else{
                String str = passwordEncoder.encode(customer.getCusPwd());
                customer.setCusPwd(str);
                customerService.update(customer);
                return "success2";
            }
        }else {
            String str = passwordEncoder.encode(customer.getCusPwd());
            customer.setCusPwd(str);
            customerService.insert(customer);
            return "success1";
        }

    }
    @PostMapping("/api/user/findUserList")
    public PageInfo<Customer> findUserlist(@RequestBody PageCondition<Customer> condition){
        return customerService.page(condition);
    }
    @PostMapping("/api/user/delete")
    public String delete(@RequestBody Customer customer){
        customerService.deleteById(customer);
        return "success";
    }
    @PostMapping("/api/user/setStatus")    //设置角色  管理员 用户
    public String setStatus(@RequestBody Customer customer){
        customerService.setStatus(customer);
        return "success";
    }
    @PostMapping("/api/user/find")
    public Customer find(@RequestBody Customer customer){
        return customerService.find(customer);
    }
    @PostMapping("/api/user/update")
    public String update(@RequestBody Customer customer){
        customerService.update(customer);
        return "success";
    }
}
